﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.Windows.Controls;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TGS08 : ViewBase
    {
        #region ==CART===========
        public static readonly DependencyProperty CARTProperty = DependencyProperty.Register("CART",
            typeof(string), typeof(TGS08),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCARTChanged));
        public string CART
        {
            get
            {
                return (string)GetValue(CARTProperty);
            }
            set
            {
                SetValue(CARTProperty, value);
                Load();
            }
        }
        private static void OnCARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGS08;
            if (src != null)
            {
                src.CART = (string)e.NewValue; 
            }
        }
        #endregion 
     
        public TGS08()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VGASTGA();
            this.DataContext = MV;
            InitializeComponent();
        }
        private void Load_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            base.Window_Loaded(sender, e);
        }
       
        public void Load()
        {
            TBMain.Clear();
            if (CART.Length < 23)
                return;
            dwData = TBMain.NewRow();
            dwData["GAGMANDANT"] = app.Mandant;
            dwData["GAGART"] = Helpers.Helper.Left(CART, 20);
            dwData["GAGVAR"] = CART.Substring(20, 3);
            dwData["GAGFARBE"] = Helper.Right(CART, 10);

             SResult = ((VGASTGA)MV).QueryGASTGA(dwData, 4);
            DataSet SReturn = new DataSet();
            SReturn.Merge(SResult);
            bool IsFound = true;
            string yarns = "", Yart = "", Yvar = "", Ycolor = "";

            while (true)
            {
                if (!IsFound)
                    break;
                IsFound = false;
                if (!SReturn.Tables.Contains(TB))
                {
                    break;
                }
                DataSet STemp = new DataSet();
                foreach (DataRow row in SReturn.Tables[TB].Rows)
                {
                    string GAGKGNET = row["GAGKGNET"].ToString();
                    if (GAGKGNET == "0010000000")
                        row["GAGKGNET"] = "0001000000";
                    string GSFERTKZ = row["GAGGART"].ToString();
                    if (GSFERTKZ != "E" && GSFERTKZ != "F" && GSFERTKZ == "X") continue;
                    Yart = row["GAGGART"].ToString();
                    Yvar = row["GAGGVAR"].ToString();
                    Ycolor = row["GAGGFARBE"].ToString();
                    if (yarns.Contains(Yart + Yvar + Ycolor + ";"))
                        continue;
                    IsFound = true;
                    yarns += Yart + Yvar + Ycolor + ";";
                    dwData["GAGMANDANT"] = app.Mandant;
                    dwData["GAGART"] = Yart;
                    dwData["GAGVAR"] = Yvar;
                    dwData["GAGFARBE"] = Ycolor;
                    SResult = ((VGASTGA)MV).QueryGASTGA(dwData, 4);
                     
                    if (SResult.Tables[0].Rows.Count == 0)
                    {
                        SResult = ((VGASTGA)MV).QueryGASTGA(dwData, 5);
                    }
                    if (SResult.Tables[0].Rows.Count == 0)
                    {
                        //SResult = MV.QueryGASTGA(dwData, 8);
                    }
                    STemp.Merge(SResult);
                }
                SReturn.Merge(STemp);
            }

            if (vm.SetProd.Tables.Contains(TB))
            {
                vm.SetProd.Tables[TB].Clear();
            }
            vm.SetProd.Tables[TB].Merge(SReturn.Tables[TB]);
            if (vwMaster.View != null)
            {
                vwMaster.View.MoveCurrentToFirst();
            }
            Yart = Helpers.Helper.Left(CART, 20);
            Yvar = CART.Substring(20, 3);
            Ycolor = Helper.Right(CART, 10);
            yarns = Yart.Trim() + "." + Yvar.Trim() + "." + Ycolor.Trim();
            BOMTree.Items.Clear();
            TreeViewItem Father = null;
            Father = new TreeViewItem();
            Father.Header = yarns;
            Father.Tag = CART;
            BOMTree.Items.Add(Father);
            SetUpBOMTree(Father, vm.SetProd.Tables[TB]);
        }
        private void SetUpBOMTree(TreeViewItem Father, DataTable _bom)
        {
            string yarn = "", Yart = "", Yvar = "", Ycolor = "";
            string _CART = (string)Father.Tag;
            Yart = Helpers.Helper.Left(_CART, 20);
            Yvar = _CART.Substring(20, 3);
            Ycolor = Helper.Right(_CART, 10);
            yarn = Yart.Trim() + "." + Yvar.Trim() + "." + Ycolor.Trim();
            TreeViewItem Child = null;

            string filter = "GAGART='" + Yart + "' AND GAGVAR='" + Yvar + "' AND GAGFARBE='" + Ycolor + "'";
            string sort = "GAGPOS ASC";
            DataRow[] rows = _bom.Select(filter, sort);
            if (rows.Length == 0)
            {
                filter = "GAGART='" + Yart + "' AND GAGVAR='" + Yvar + "'"; 
               rows = _bom.Select(filter, sort);
            }
            if (rows.Length == 0)
            {
                filter = "GAGART='" + Yart + "'"; 
               rows = _bom.Select(filter, sort);
            }
            foreach (DataRow row in rows)
            { 
                Yart = row["GAGGART"].ToString();
                Yvar = row["GAGGVAR"].ToString();
                Ycolor = row["GAGGFARBE"].ToString();
                yarn = Yart.Trim() + "." + Yvar.Trim() + "." + Ycolor.Trim();
                double GAGKGNET =Convert.ToDouble(row["GAGKGNET"].ToString())/10000;
                Child = new TreeViewItem();
                Child.Header = yarn + "[" + GAGKGNET+"]";
                Child.Tag = Yart + Yvar + Ycolor;
                Father.Items.Add(Child);
                //row.Delete();
            } 
            foreach (TreeViewItem child in Father.Items)
            {
                SetUpBOMTree(child, _bom);
            } 
        }
    }
}
